<template>
    <div class="comment">
    <div class="addcomment" v-show='!isFocus'>
        <input type="text" placeholder="写跟帖" @focus="showTextarea" />
        <span class="comment" @click="toComment">
            <i class="iconfont iconpinglun-"></i>
            <em>100</em> 
        </span>
        <i class="iconfont iconshoucang"></i>
        <i class="iconfont iconfenxiang"></i>
    </div>
    <!-- 用户输入内容进行发表评论的结构 -->
    <div class="inputcomment" v-show='isFocus'>
        <textarea v-model="content" ref='commtext' rows="5" ></textarea>
        <div>
            <span @click="sendFn">发 送</span>
            <span>取 消</span>
        </div>
    </div>
</div>
</template>

<script>
import { publish } from "@/api/article.js"
export default {
    data(){
        return {
            isFocus: false,
            content: "",
            parent_id: null
        }
    },
    methods: {
        // 发表评论
        sendFn(){
            if(!this.content){
                this.$toast.fail("评论内容不能为空！")
                return
            }
            let query = {
                content: this.content
            }
            // 给query动态添加parent_id属性，因此只有回复评论的时候才需要传递这个属性给后台
            if(this.parent_id){
                query.parent_id = this.parent_id;
            }
            publish(this.$route.query.id, query).then(res=>{
                console.log(38,res);
                // 发表成功后需要清空parent_id 内容 隐藏文本域
                this.parent_id = null;
                this.content = "";
                this.isFocus =false;
                // 更新评论列表数据，因为评论列表在父组件中
                // 所以这里通过“子传父”通知父组件更新数据接口
                this.$emit("updateList")
            })
        },
        // 跳转到精彩评论页面
        toComment(){
            this.$router.push({
                path: "/comment",
                query: {
                    id: this.$route.query.id
                }
            })
        },
        showTextarea(){
            this.isFocus = true;
            // 文本框自动聚焦
            this.$nextTick(()=>{
                this.$refs.commtext.focus()
            })
        }
    }
}
</script>
<style lang="less" scoped>
.comment{
    position: fixed;
    width: 100%;
    bottom: 0;
}
.inputcomment{
    padding: 10px;
    box-sizing: border-box;
    width: 100%;
    display: flex;
    background-color: #fff;
    align-items: flex-end;
    textarea{
        flex: 3;
        background-color: #eee;
        border:none;
        border-radius: 10px;
        padding: 10px;
    }
    div{
        padding: 20px;
    }
    span {
        display: block;
        flex: 1;
        height: 24px;
        line-height: 24px;
        padding: 0 10px;
        background-color: #f00;
        color:#fff;
        text-align: center;
        border-radius: 6px;
        font-size: 13px;
    }
}
.addcomment {
  width: 100%;
  background: #fff;
  box-sizing: border-box;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  > i {
    flex: 1;
  }
}
</style>